package cn.caojun.org.controller;

import cn.caojun.auth.annotation.caojunPermission;
import cn.caojun.basic.utils.AjaxResult;
import cn.caojun.basic.utils.PageList;
import cn.caojun.org.domain.Department;
import cn.caojun.org.query.DepartmentQuery;
import cn.caojun.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/department")
@Api(value = "部门管理",description = "部门管理部分的描述")
@caojunPermission(name = "部门管理",desc = "部门管理权限 ")
public class DepartmentController {

    @Autowired
    private IDepartmentService iDepartmentService;

    @PutMapping
    @ApiOperation(value="新增或修改")
    @caojunPermission(name = "新增或修改",desc = "新增或修改管理权限 ")
    public AjaxResult addOrUpdata(@RequestBody Department department){
        try{
            if (Objects.isNull(department.getId())){
                iDepartmentService.add(department);
            }else {
                iDepartmentService.update(department);
            }
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value="根据id删除部门")
    @caojunPermission(name = "根据id删除部门",desc = "根据id删除部门权限 ")
    public AjaxResult removeById(@ApiParam(value = "主键",required = true)@PathVariable("id")Long id){
        try {
            iDepartmentService.removeById(id);
            return AjaxResult.me();
        }catch (Exception e){
            System.out.println(e.getMessage());
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value="根据id获得部门")
    @caojunPermission(name = "根据id获得部门",desc = "根据id获得部门权限 ")
    public AjaxResult getById(@ApiParam(value = "主键",required = true) @PathVariable("id")Long id){
        //@PathVariable映射URL绑定的占位符，将URL中的占位符参数绑定到控制器的方法进行入参时，URL中{xxx}占位符可以通过
        // @Pathvariable(“XXX”)进行绑定。一般是在get请求中使用。
        try{
            Department department=iDepartmentService.getById(id);
            return AjaxResult.me().setObject(department);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }



    @GetMapping
    @ApiOperation(value="获得全部部门")
    @caojunPermission(name = "获得全部部门",desc = "获得全部部门权限 ")
    public AjaxResult getAll(){
        try{
            List<Department> department=iDepartmentService.getAll();
            return AjaxResult.me().setObject(department);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping
    @caojunPermission(name = "分页获取列表",desc = "分页获取列表权限 ")
    public AjaxResult getDepartmentList(@RequestBody DepartmentQuery departmentQuery){
        try{
            PageList<Department> department=iDepartmentService.getLimteList(departmentQuery);
            return AjaxResult.me().setObject(department);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PatchMapping
    @Transactional
    @caojunPermission(name = "批量删除",desc = "批量删除权限 ")
    public AjaxResult pathDelete(@RequestBody List<Long> ids){
        try {
            iDepartmentService.batchRemove(ids);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping("/tree")
    @caojunPermission(name = "获取部门树",desc = "获取部门树权限 ")
    public AjaxResult getDepartmentTree(){
        try {
            List<Department> departmentList=iDepartmentService.getTree();
            return AjaxResult.me().setObject(departmentList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
}
